# 16. 整数转罗马数字
import math
from typing import List

class Solution:
    def intToRoman(self, num: int) -> str:
        map = {0:'', 1:'I', 5:'V', 10:'X', 50:'L', 100:'C', 500:'D', 1000:'M'}
        res = ''
        length = len(str(num))
        for i in str(num):
            if int(i) >= 5:
                if int(i) == 9:
                    res += map[1*math.pow(10, length-1)] + map[10*math.pow(10, length-1)]
                else:
                    res += map[5*math.pow(10, length-1)] + map[1*math.pow(10, length-1)]*(int(i)-5)
            else:
                if int(i) == 4:
                    res += map[1*math.pow(10, length-1)] + map[5*math.pow(10, length-1)]
                else:
                    res += map[1 * math.pow(10, length - 1)] * int(i)
            # 每处理完一个数字降阶一次
            length -= 1
        return res